//
//  Account.swift
//  XHWalletUtils
//
//  Created by xinph on 2020/5/20.
//  Copyright © 2020 xinph. All rights reserved.
//

import Foundation
import WCDBSwift

public protocol AccountType {
    /// 账户名称
    var name: String? { get set }
    /// 地址
    var address: String { get set }
    /// 多签地址
    var multiAddress: String? { get set }
    /// 币种类型 BIP44 https://github.com/satoshilabs/slips/blob/master/slip-0044.md
    var coinType: UInt64 { get }
}

public final class Account: AccountType & Equatable {
    public var multiAddress: String?
    public var name: String?
    public var address: String
    public var coinType: UInt64
    

    init(_ name: String? = nil, address: String, coin: UInt64) {
        self.name = name
        self.address = address
        self.coinType = coin
    }

    public static func == (lhs: Account, rhs: Account) -> Bool {
        return lhs.address.lowercased() == rhs.address.lowercased() &&
            lhs.coinType == rhs.coinType
    }
}

public protocol AddressConvertible {
    /// 地址
    func asAddress() -> String
}

extension String: AddressConvertible {
    public func asAddress() -> String { return self }
}

extension Account: AddressConvertible {
    public func asAddress() -> String { return self.address }
}

/// WCDB
extension Account: ColumnJSONCodable {}
